﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCPnuts.ISkyDiskFileSystem.Interfaces;
using CCPnuts.ISkyDiskFileSystem.Exceptions;
using CCPnuts.Pack;

namespace CCPnuts.ISkyDiskFileSystem
{
    public abstract class SkyElement : PackerBase
    {
        #region Packed Target

        internal new I_SkyElement Packed { get { return (I_SkyElement)base.Packed; } }

        internal SkyElement(IPacked tar) : base(tar) { }

        #endregion

        #region Proxy Members

        #region Essential members

        /// <summary>
        /// Gets or sets element name
        /// </summary>
        public string Name
        {
            get { return Packed.Name; }
            set { Packed.Name = value; }
        }

        /// <summary>
        /// gets the parent of this element
        /// </summary>
        public SkyElementContainer Parent
        {
            get { return (SkyElementContainer)Util.NNTar(Packed.Parent); }
        }

        /// <summary>
        /// Copy this element to the target directory
        /// </summary>
        /// <param name="tarDir"></param>
        public void CopyTo(SkyElementContainer tarDir)
        {
            Packed.CopyTo((I_SkyElementContainer)Util.NNTar(tarDir));
        }

        /// <summary>
        /// Download this element.
        /// </summary>
        /// <param name="targetPath"></param>
        /// <returns></returns>
        public Downloader Download(string targetPath)
        {
            return (Downloader)Util.NNTar(Packed.Download(targetPath));
        }

        #endregion

        #region Optional Members

        /// <summary>
        /// Move this element to the target directory
        /// </summary>
        /// <param name="tarDir"></param>
        [Optional]
        public void MoveTo(SkyElementContainer tarDir)
        {
            try
            {
                Packed.MoveTo((I_SkyElementContainer)Util.NNTar(tarDir));
            }
            catch (NotImplementedException)
            {
                if (tarDir == null)
                {
                    throw new ArgumentNullException("The argument can not be null.");
                }
                if (tarDir is SkyDisk)
                {
                    throw new SkyElementException("Can not move a SkyDisk.");
                }
                SkyElementContainer sd = Parent;
                CopyTo(tarDir);
                //sd.Delete(this);
            }
        }

        #endregion

        #endregion

        #region Own Members

        public abstract SkyElementTypes ElementType { get; }

        #endregion
    }
}
